<template>
  <div class="message">
    <el-popover placement="bottom" :width="310" trigger="click">
      <template #reference>
        <el-badge :value="totalCount" class="item">
          <i :class="'iconfont icon-xiaoxi'" class="toolBar-icon"></i>
        </el-badge>
      </template>
      <el-tabs v-model="activeName">
        <el-tab-pane :label="`公告(` + unreadAnnouncementCount + `)`" name="ANNOUNCEMENT">
          <div v-if="filteredMessages.length != 0" class="message-list">
            <div v-for="message in filteredMessages" :key="message.id" class="message-item" @click="markAsRead(message)">
              <img src="@/assets/images/msg03.png" alt="" class="message-icon" />
              <div class="message-content">
                <span class="message-title" :class="{ 'message-read': message.read }"> {{ message.title }}</span>
                <span class="message-date">{{ message.createTime }}</span>
              </div>
            </div>
          </div>
          <div v-else class="message-empty">
            <img src="@/assets/images/notData.png" alt="notData" />
            <div>暂无公告</div>
          </div>
        </el-tab-pane>
        <el-tab-pane :label="`提醒(` + unreadRemindCount + `)`" name="REMIND">
          <div v-if="filteredMessages.length != 0" class="message-list">
            <div v-for="message in filteredMessages" :key="message.id" class="message-item" @click="markAsRead(message)">
              <img src="@/assets/images/msg01.png" alt="" class="message-icon" />
              <div class="message-content">
                <span class="message-title" :class="{ 'message-read': message.read }"> {{ message.title }}</span>
                <span class="message-date">{{ message.createTime }}</span>
              </div>
            </div>
          </div>
          <div v-else class="message-empty">
            <img src="@/assets/images/notData.png" alt="notData" />
            <div>暂无提醒</div>
          </div>
        </el-tab-pane>
        <el-tab-pane :label="`评论(` + unreadCommentCount + `)`" name="COMMENT">
          <div v-if="filteredMessages.length != 0" class="message-list">
            <div v-for="message in filteredMessages" :key="message.id" class="message-item" @click="markAsRead(message)">
              <img src="@/assets/images/msg02.png" alt="" class="message-icon" />
              <div class="message-content">
                <span class="message-title" :class="{ 'message-read': message.read }"> {{ message.title }}</span>
                <span class="message-date">{{ message.createTime }}</span>
              </div>
            </div>
          </div>
          <div v-else class="message-empty">
            <img src="@/assets/images/notData.png" alt="notData" />
            <div>暂无评论</div>
          </div>
        </el-tab-pane>
      </el-tabs>
    </el-popover>
  </div>
</template>

<script setup lang="ts">
import { computed, onMounted, ref } from "vue";
import { getMessageList, updateMessage } from "@/api/modules/message";
import { Message } from "@/api/interface";

const activeName = ref("ANNOUNCEMENT");

const messages = ref<Message.ResMessage[]>([]);

const filteredMessages = computed(() => {
  return messages.value.filter(message => message.messageType === activeName.value);
});

// 未读总消息
const totalCount = computed(() => {
  return messages.value.filter(message => message.read === 0).length;
});

// 未读评论
const unreadCommentCount = computed(() => {
  return messages.value.filter(message => message.messageType === "COMMENT" && message.read === 0).length;
});

// 未读提醒
const unreadRemindCount = computed(() => {
  return messages.value.filter(message => message.messageType === "REMIND" && message.read === 0).length;
});

const unreadAnnouncementCount = computed(() => {
  return messages.value.filter(message => message.messageType === "ANNOUNCEMENT" && message.read === 0).length;
});
// 将消息标记为已读
const markAsRead = async (message: Message.ResMessage) => {
  if (message.read === 1) {
    return;
  }
  await updateMessage({
    id: message.id,
    read: 1
  });
  await getMessages();
};

const getMessages = async () => {
  const { data } = await getMessageList();
  console.log(data);
  messages.value = data;
};

onMounted(() => getMessages());
</script>

<style scoped lang="scss">
.message-empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 260px;
  line-height: 45px;
}
.message-list {
  display: flex;
  flex-direction: column;
  max-height: 260px; /* 设置最大高度，超过部分会出现滚动条 */
  overflow-y: auto; /* 添加滚动效果 */
  .message-item {
    display: flex;
    align-items: center;
    padding: 20px 0;
    border-bottom: 1px solid var(--el-border-color-light);
    &:last-child {
      border: none;
    }
    .message-icon {
      width: 40px;
      height: 40px;
      margin: 0 20px 0 5px;
    }
    .message-content {
      display: flex;
      flex-direction: column;
      .message-title {
        margin-bottom: 5px;
      }
      .message-date {
        font-size: 12px;
        color: var(--el-text-color-secondary);
      }

      /* 已读消息样式 */
      .message-read {
        color: rgb(163 166 173); /* 已读消息的文本颜色 */
      }
    }
  }
}
</style>
